<template>
  <div class="prescription-print-view" v-if="presItem">
    <!-- 成药处方打印 -->
    <div
      class="print-main"
      style="padding: 20px; page-break-after: always"
      v-if="presItem.category_key == 'cycf'"
    >
      <prescription-header :patientData="patientData"></prescription-header>
      <div class="prescription-container">
        <cy-prescription-list
          :prescriptionItem="presItem"
        ></cy-prescription-list>
      </div>
      <prescription-footer :patientData="footerData"></prescription-footer>
    </div>
    <!-- 饮片处方打印 -->
    <div
      class="print-main"
      style="padding: 20px; page-break-after: always"
      v-if="presItem.category_key == 'ypcf'"
    >
      <prescription-header :patientData="patientData"></prescription-header>
      <div class="prescription-container">
        <yp-prescription-list
          :prescriptionItem="presItem"
        ></yp-prescription-list>
      </div>
      <prescription-footer :patientData="footerData">
        <template slot="yipian-footer">
          <div class="yipian-footer">
            <div class="left">
              <span class="left-title">用量：</span>
              <p>
                <span>{{ footerData.remark.yf }}</span
                ><span>{{ footerData.remark.mrjl }}剂/日</span>
              </p>
            </div>
            <div class="right">
              <span class="left-title">用法：</span>
              <p>
                <span>{{ footerData.remark.yf }}</span>
                <span>{{ footerData.remark.fyyq }}</span>
              </p>
            </div>
          </div>
        </template>
      </prescription-footer>
    </div>
    <!-- 输液处方打印 -->
    <div
      class="print-main"
      style="padding: 20px; page-break-after: always"
      v-if="presItem.category_key == 'sycf'"
    >
      <prescription-header :patientData="patientData"></prescription-header>
      <div class="prescription-container">
        <sy-prescription-list
          :prescriptionItem="shuyeItem"
        ></sy-prescription-list>
      </div>
      <prescription-footer :patientData="footerData"></prescription-footer>
    </div>
  </div>
</template>
<script>
import prescriptionHeader from "./prescriptionHeader.vue";
import prescriptionFooter from "./prescriptionFooter.vue";
import checkItemList from "./checkItemList.vue";
import cyPrescriptionList from "./cyPrescriptionList.vue";
import ypPrescriptionList from "./ypPrescriptionList.vue";
import syPrescriptionList from "./syPrescriptionList.vue";
export default {
  components: {
    prescriptionHeader,
    prescriptionFooter,
    checkItemList,
    cyPrescriptionList,
    ypPrescriptionList,
    syPrescriptionList,
  },
  props: {
    patientData: {
      type: Object,
      default: () => {},
    },
    presItem: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      footerData: null,
      shuyeItem: null
    };
  },
  watch: {
    presItem: {
      handler(newval, oldval) {
        if (newval) {
          if(newval.age){
            newval.age = newval.age.split(",").map((value)=>{return Number(value)})
          }
          this.footerData = {
            amount: 0,
            doctor_name: this.patientData.doctor_name,
          };
          let totalPrice = 0;
          if (newval.category_key == "cycf") {
            newval.details.forEach((itm, idx) => {
              totalPrice += Number(itm.number) * Number(itm.price);
            });
            this.$set(this.footerData, "amount", totalPrice);
          } else if (newval.category_key == "ypcf") {
            newval.details.forEach((itm, idx) => {
              totalPrice += Number(itm.number) * Number(itm.price);
            });
            this.$set(this.footerData, "amount", totalPrice);
            this.$set(this.footerData, "remark", newval.remark);
          } else {
            
            this.shuyeItem = {
              ID: newval.ID,
              update_status: newval.update_status,
              prescription_name: "",
              category_key: "sycf",
              category_name: "输液处方",
              details: [],
              remark: {},
              group_count: newval.group.length,
            };
            newval.group.forEach((gitm, gidx) => {
              let detail = JSON.parse(JSON.stringify(gitm.details));
              detail.forEach((m, i) => {
                m.number = Number(m.number);
                m.price = Number(m.price);
                m.amount = Number(m.amount);
                totalPrice += m.amount
                this.$set(this.footerData, "amount", totalPrice);
                m.group = gidx + 1;
                if (!m.product_name) {
                  detail.splice(i, 1);
                } else {
                  this.shuyeItem.details.push(m);
                }
              });
              Object.assign(this.shuyeItem.remark, { [gidx + 1]: gitm.remark });
            });
          }
        }
      },
      immediate: true,
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/scss/print/prescriptionPrint.scss";
</style>